/*
 *  Copyright 2010 Andrew Kroh
 *
 *  Kroh-IO is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Kroh-IO is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>

#include "serial_port.h"
#include "util.h"

int main(int argc, const char* argv[])
{
    if (argc != 2)
    {
        printf("Usage: %s [serial-port]\n", argv[0]);
        return 1;
    }
    
    int rtn = -1;
    const char* serial_port = argv[1];
    
    set_debug_level(DEBUG_TRACE);
    
    printf("Calling test_read() on <%s>.\n", serial_port);
    int test_success = test_serial_port(serial_port);
    printf("test_read() returned %d\n\n", test_success);
    
    if (test_success)
    {
        printf("Calling open_port() on <%s>.\n", serial_port);
        int file_descriptor = open_port(serial_port);
        printf("open_port() returned %d\n\n", file_descriptor);
        
        if (file_descriptor > 0)
        {
            printf("Calling get_baud_rate() on <%d>.\n", file_descriptor);
            int baud_rate = get_baud_rate(file_descriptor);
            printf("get_baud_rate() returned %d\n\n", baud_rate);
            
            printf("Calling close_port() on <%d>.\n", file_descriptor);
            int close_rtn = close_port(file_descriptor);
            printf("close_port() returned %d\n\n", close_rtn);
            
            if (close_rtn == 0)
            {
                // Success!
                rtn = 0;
            }
        }
    }
    
    if (!rtn)
    {
        printf("Test PASSED for port <%s>.\n", serial_port);
    }
    else
    {
        printf("Test FAILED for port <%s>.\n", serial_port);
    }
    
    return rtn;
}
